package com.things8.iot.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.things8.iot.common.enums.GlobalResultEnum;
import com.things8.iot.common.result.GlobalException;

import java.lang.reflect.Field;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/5/2 23:44
 */
public class PageUtil<T> {

    public static<T> Page<Object> getObjectPage(T t) {
        try {
            Page<Object> page = new Page<>();
            Class<?> superclass = t.getClass().getSuperclass();
            Field pageNum0 = superclass.getDeclaredField("pageNum");
            Field pageSize0 = superclass.getDeclaredField("pageSize");
            pageNum0.setAccessible(true);
            pageSize0.setAccessible(true);
            Integer pageNum = (Integer) pageNum0.get(t);
            Integer pageSize = (Integer) pageSize0.get(t);
            if (pageNum != null) {
                page.setCurrent(pageNum);
            }
            if (pageSize != null) {
                page.setSize(pageSize);
            }
            return page;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            throw new GlobalException(GlobalResultEnum.PAGE_PARAM_ERROR);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new GlobalException(GlobalResultEnum.PAGE_PARAM_ERROR);
        }
    }

}
